package com.wxm.killing.mq.startup;


import com.wxm.killing.mq.dao.TbSysLockDao;
import com.wxm.killing.mq.dao.TbUserDao;
import com.wxm.killing.mq.entity.TbSysLock;
import com.wxm.killing.mq.entity.TbUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;


/**
 * @Description 项目启动时调用
 * @author: wxm
 * @date: 2019/10/23 23:28
 * @version: 1.0
 */
//@Component
public class StartupLoading implements CommandLineRunner {

    private static Logger LOGGER = LoggerFactory.getLogger(StartupLoading.class);

    public static Set<String> ipSet = new HashSet<String>();

    public static Set<String> tokenSet = new HashSet<String>();

    @Autowired
    private TbSysLockDao tbSysLockDao;

    @Autowired
    private TbUserDao tbUserDao;

    @Override
    public void run(String... strings) throws Exception {
        while(true){
            //获取被封的用户
            List<TbSysLock> tbSysLockList = tbSysLockDao.selectAll();
            for(TbSysLock tbSysLock :tbSysLockList){
                ipSet.add(tbSysLock.getIp());
            }
            getToken(tbSysLockList);
            //睡眠5分钟
            Thread.sleep(1000*60*5);
        }

    }

    private void getToken(List<TbSysLock> tbSysLockList) {
        List<String> userNameList = new ArrayList<>();
        for(TbSysLock tbSysLock :tbSysLockList){
            userNameList.add(tbSysLock.getUserName());
        }
        if(CollectionUtils.isEmpty(userNameList)){
            return;
        }
        List<TbUser> tbUserList = tbUserDao.selectByUserNames(userNameList);
        for(TbUser tbUser :tbUserList){
            tokenSet.add(tbUser.getToken());
        }
    }

}
